package com.os.util;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

public class Md5Util {
	public static void md5(StringBuffer result, String password, Object salt) {
		Md5PasswordEncoder md5 = new Md5PasswordEncoder();
		md5.setEncodeHashAsBase64(true);
		result.append(md5.encodePassword(password, salt));
	}
	/**
	 * 加密
	 * @param type
	 * @param password
	 * @param salt
	 * @return
	 */
	public static String encodePassword(String type, String password, Object salt) {
		StringBuffer result = new StringBuffer();
		md5(result, password, salt);
		return result.toString();
	}

	/**
	 * 解密
	 * 
	 * @param secretkey
	 * @param password
	 * @param salt
	 * @return
	 */
	public static boolean decodePassword(String secretkey, String password, Object salt) {
		StringBuffer result = new StringBuffer();

		md5(result, password, salt);

		// 验证通过
		if (result.toString().equals(secretkey)) {
			return true;
		}
		return false;
	}
}
